/**
  ******************************************************************************
  * @file    Templates/Src/main.c 
  * @author  MCD Application Team
  * @version V1.0.1
  * @date  09-October-2015
  * @brief   Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
  *
  * Redistribution and use in source and binary forms, with or without modification,
  * are permitted provided that the following conditions are met:
  *   1. Redistributions of source code must retain the above copyright notice,
  *      this list of conditions and the following disclaimer.
  *   2. Redistributions in binary form must reproduce the above copyright notice,
  *      this list of conditions and the following disclaimer in the documentation
  *      and/or other materials provided with the distribution.
  *   3. Neither the name of STMicroelectronics nor the names of its contributors
  *      may be used to endorse or promote products derived from this software
  *      without specific prior written permission.
  *
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "arm_math.h"

/** @addtogroup STM32F4xx_HAL_Examples
  * @{
  */

/** @addtogroup Templates
  * @{
  */
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
static void SystemClock_Config(void);
static void Error_Handler(void);
static void LCD_Config(void);
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{

  /* STM32F4xx HAL library initialization:
  - Configure the Flash prefetch and Buffer caches
  - Systick timer is configured by default as source of time base, but user 
  can eventually implement his proper time base source (a general purpose 
  timer for example or other time source), keeping in mind that Time base 
  duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and 
  handled in milliseconds basis.
  - Low Level Initialization
  */
  HAL_Init();
  /* Configure the system clock to 180 MHz */
  SystemClock_Config();

  /* Add your application code here*/
  Gpio_Init();
  LCD_Config();
  HAL_Delay(1000);
  PcdReset();
  PcdAntennaOn();
  /* Infinite loop */
  BSP_LCD_Clear(LCD_COLOR_BLACK);
  BSP_LCD_SetFont(&LCD_DEFAULT_FONT);
  BSP_LCD_SetBackColor(LCD_COLOR_BLACK);
  BSP_LCD_Clear(LCD_COLOR_BLACK);
  BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
  
  volatile float data = 1.15798f;
  float data1;
  printf("This is STM32F4-FPU Test\n");
  while (1)
  {
    data = data * 2.1f;
    arm_sqrt_f32(data,&data1);
    printf("%f\n",data1);
//    HAL_Delay(1);
  }
}
/**
  * @brief  LCD Configuration.
  * @retval None
  */

static void LCD_Config(void)
{
  uint8_t lcd_status = LCD_OK;

  /* LCD DSI initialization */
  lcd_status = BSP_LCD_Init();
  if(lcd_status != LCD_OK)
  {
    Error_Handler();
  }
  
  BSP_LCD_LayerDefaultInit(0, LCD_FB_START_ADDRESS);
  BSP_LCD_SelectLayer(0);  

  /* Clear the LCD Background layer */
  BSP_LCD_Clear(LCD_COLOR_BLACK);

  BSP_LCD_SetFont(&LCD_DEFAULT_FONT);

  /* Clear the LCD */
  BSP_LCD_SetBackColor(LCD_COLOR_BLACK);
  BSP_LCD_Clear(LCD_COLOR_BLACK);

  /* Set the LCD Text Color */
  BSP_LCD_SetTextColor(LCD_COLOR_WHITE);

  /* Display LCD messages */
  BSP_LCD_DisplayStringAt(0, 0, (uint8_t *)"System is initialling...", LEFT_MODE);
}


/**
  * @brief  System Clock Configuration
  *         The system Clock is configured as follow : 
  *            System Clock source            = PLL (HSE)
  *            SYSCLK(Hz)                     = 180000000
  *            HCLK(Hz)                       = 180000000
  *            AHB Prescaler                  = 1
  *            APB1 Prescaler                 = 4
  *            APB2 Prescaler                 = 2
  *            HSE Frequency(Hz)              = 8000000
  *            PLL_M                          = 8
  *            PLL_N                          = 360
  *            PLL_P                          = 2
  *            PLL_Q                          = 7
  *            PLL_R                          = 6
  *            VDD(V)                         = 3.3
  *            Main regulator output voltage  = Scale1 mode
  *            Flash Latency(WS)              = 5
  * @param  None
  * @retval None
  */
static void SystemClock_Config(void)
{
  RCC_ClkInitTypeDef RCC_ClkInitStruct;
  RCC_OscInitTypeDef RCC_OscInitStruct;

  /* Enable Power Control clock */
  __HAL_RCC_PWR_CLK_ENABLE();
  __PWR_CLK_ENABLE();
  /* The voltage scaling allows optimizing the power consumption when the device is 
     clocked below the maximum system frequency, to update the voltage scaling value 
     regarding system frequency refer to product datasheet.  */
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

  /* Enable HSE Oscillator and activate PLL with HSE as source */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;

  RCC_OscInitStruct.PLL.PLLM = 4;
  RCC_OscInitStruct.PLL.PLLN = 180;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 4;
  RCC_OscInitStruct.PLL.PLLR = 2;
  
  if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  /* Enable the OverDrive to reach the 180 Mhz Frequency */  
  if(HAL_PWREx_EnableOverDrive() != HAL_OK)
  {
    Error_Handler();
  }
  
  /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 
     clocks dividers */
  RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV16;  
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV16; 

  if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  {
    Error_Handler();
  }
}

/**
  * @brief  This function is executed in case of error occurrence.
  * @param  None
  * @retval None
  */
static void Error_Handler(void)
{
  /* User may add here some code to deal with this error */
  while(1)
  {
  }
}
void Auto_Reader(void)
{
  uint8_t i,j;
  while(1)
  {
    if(PcdRequest(0x52,Temp)==MI_OK)
    {
      if(Temp[0]==0x04&&Temp[1]==0x00)  
         BSP_LCD_DisplayStringAt(0, 0, (uint8_t *)"MFOne-S50", LEFT_MODE);
        else if(Temp[0]==0x02&&Temp[1]==0x00)
          BSP_LCD_DisplayStringAt(0, 0, (uint8_t *)"MFOne-S70", LEFT_MODE);
        else if(Temp[0]==0x44&&Temp[1]==0x00)
          BSP_LCD_DisplayStringAt(0, 0, (uint8_t *)"MF-UltraLight", LEFT_MODE);
        else if(Temp[0]==0x08&&Temp[1]==0x00)
          BSP_LCD_DisplayStringAt(0, 0, (uint8_t *)"MF-Pro", LEFT_MODE);
        else if(Temp[0]==0x44&&Temp[1]==0x03)
          BSP_LCD_DisplayStringAt(0, 0, (uint8_t *)"MF Desire", LEFT_MODE);
        else
          BSP_LCD_DisplayStringAt(0, 0, (uint8_t *)"Unknown", LEFT_MODE);
      if(PcdAnticoll(UID)==MI_OK)
      { 
        BSP_LCD_DisplayStringAt(0, 30, (uint8_t *)"Card Id is:", LEFT_MODE);
        for(i = 0; i < 4;i++)
          changeid(UID[i],i);
        for(j = 0;j < 10;j++)
          BSP_LCD_DisplayChar(j*15,60,tochar(ID[j]));  
      }
    }
  }
}
void Find_Card(void)
{
   if(PcdRequest(0x52,Temp)==MI_OK)
    {
      if(Temp[0]==0x04&&Temp[1]==0x00)  
         BSP_LCD_DisplayStringAt(0, 0, (uint8_t *)"MFOne-S50", LEFT_MODE);
        else if(Temp[0]==0x02&&Temp[1]==0x00)
          BSP_LCD_DisplayStringAt(0, 0, (uint8_t *)"MFOne-S70", LEFT_MODE);
        else if(Temp[0]==0x44&&Temp[1]==0x00)
          BSP_LCD_DisplayStringAt(0, 0, (uint8_t *)"MF-UltraLight", LEFT_MODE);
        else if(Temp[0]==0x08&&Temp[1]==0x00)
          BSP_LCD_DisplayStringAt(0, 0, (uint8_t *)"MF-Pro", LEFT_MODE);
        else if(Temp[0]==0x44&&Temp[1]==0x03)
          BSP_LCD_DisplayStringAt(0, 0, (uint8_t *)"MF Desire", LEFT_MODE);
        else
          BSP_LCD_DisplayStringAt(0, 0, (uint8_t *)"Unknown", LEFT_MODE);
        BSP_LCD_DisplayStringAt(0, 20, (uint8_t *)"Success!", LEFT_MODE);
    }
   else
    BSP_LCD_DisplayStringAt(0, 20, (uint8_t *)"Faile!", LEFT_MODE);                                           
}
void Gpio_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStruct;
   
  __HAL_RCC_GPIOA_CLK_ENABLE();
  __HAL_RCC_GPIOB_CLK_ENABLE();
  __HAL_RCC_GPIOD_CLK_ENABLE();
  __HAL_RCC_GPIOH_CLK_ENABLE();
  
  GPIO_InitStruct.Pin = GPIO_PIN_7;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
  HAL_GPIO_Init(GPIOA,&GPIO_InitStruct);
  
  GPIO_InitStruct.Pin = GPIO_PIN_6;
  HAL_GPIO_Init(GPIOH,&GPIO_InitStruct);
  
  GPIO_InitStruct.Pin = GPIO_PIN_3;
  HAL_GPIO_Init(GPIOD,&GPIO_InitStruct);
  
  GPIO_InitStruct.Pin = GPIO_PIN_15;
  HAL_GPIO_Init(GPIOB,&GPIO_InitStruct);
  
  GPIO_InitStruct.Pin = GPIO_PIN_14;
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
  HAL_GPIO_Init(GPIOB,&GPIO_InitStruct);
}
void changeid(unsigned char UID,unsigned char i)
{
  uint8_t temp,temp1;
  temp = UID;
  temp1 = UID;
  temp = temp & 0x0f;
  temp1 = temp1 & 0xf0;
  temp1 = temp1 >> 4;
  ID[2*i] = temp1;
  ID[2*i+1] = temp; 
}
uint8_t tochar(uint8_t id)
{
    uint8_t result;
    switch(id) 
    {
    case 0x00:result = '0';break;
    case 0x01:result = '1';break;
    case 0x02:result = '2';break;
    case 0x03:result = '3';break;
    case 0x04:result = '4';break;
    case 0x05:result = '5';break;
    case 0x06:result = '6';break;
    case 0x07:result = '7';break;
    case 0x08:result = '8';break;
    case 0x09:result = '9';break;
    case 0x0a:result = 'a';break;
    case 0x0b:result = 'b';break;
    case 0x0c:result = 'c';break;
    case 0x0d:result = 'd';break;
    case 0x0e:result = 'e';break;
    case 0x0f:result = 'f';break;
    }
    return result;  
}

#ifdef  USE_FULL_ASSERT

/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{ 
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {
  }
}
#endif

/**
  * @}
  */ 

/**
  * @}
  */ 

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
